using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework;

namespace Elementum
{
    class SnowBall : Actor
    {
        float _radius;

        public int LifeTime
        {
            get
            { return _lifeTime; }
            set
            {
                _lifeTime = value;
                lifeCount = 60 * _lifeTime;
            }
        }
        int _lifeTime;

        int lifeCount;

        public bool IsLife { get; set; }

      public  SnowBall(World world, Stage stage,float scale)
            : base(world, stage)
        {
            //Random rnd = new Random(Guid.NewGuid().GetHashCode());
            float i ;
            i = scale;
            CurrentAnimation = new Animation("images/items/snowball");
            CurrentAnimation.LayerDepth = 0.6f;
            Size = new Vector2(108*i, 108*i);
            Scale = i;
            LifeTime = 5;
            
            IsLife = true;
        }


        public override void CreatBody(World world)
        {
            base.CreatBody(world);
            _body.Position = new Vector2(300 / PIXEL_TO_METER, 200 / PIXEL_TO_METER);
            _body.SetType(BodyType.Dynamic);
            _body.SetUserData("snowball");
            //_body.SetBullet(true);
        }

        public override void Restart()
        {
            _body.SetUserData("snowball");
        }

        protected override void SetSize(Vector2 size)
        {
            _size = size;
            _radius = _size.X / (2 * PIXEL_TO_METER);
            if (_fixture == null)
            {
                FixtureDef f = new FixtureDef();
                CircleShape p = new CircleShape();
                //PolygonShape p = new PolygonShape();
                f.shape = p;
                f.density = 175;
                f.restitution = 0.4f;
                f.friction = 0.60f;
                p._radius = _radius;
                //p.SetAsBox(_size.X / (PIXEL_TO_METER * 2), _size.Y / (PIXEL_TO_METER * 2));
               // p.SetAsBox(_size.X / (PIXEL_TO_METER * 2), _size.Y / (PIXEL_TO_METER * 2));
                this._fixture = _body.CreateFixture(f);
            }
          
            _fixture.SetFilterData(ref _filter);
        }

        protected override void ConfigFilter()
        {
            base.ConfigFilter();

            _filter.categoryBits = SnowBallCategory;
            _filter.maskBits=SnowBallMask;
        }

        public override void BeginContactAction(Contact contact, Body contactBody, float normal)
        {
            base.BeginContactAction(contact, contactBody, normal);
        }

        public override void EndContactAction(Contact contact, Body contactBody, float normal)
        {
            base.EndContactAction(contact, contactBody, normal);
        }

        public override void Update(int delta)
        {
            base.Update(delta);
            if (IsLife && lifeCount > 0)
            {
                lifeCount--;
                if (lifeCount <= 100)
                {
                    this.Alpha -= 0.01f;
                }

                if (lifeCount == 0)
                {
                    IsLife = false;
                    lifeCount = 30 * LifeTime;
                }
            }
        }
    }
}
